package com.kuiandjie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kuiandjie.entity.Goal;
import com.kuiandjie.util.DBHelp;
import com.kuiandjie.util.RowMapper;

public class GoalDao {
	private DBHelp<Goal> db = new DBHelp<Goal>();
	
	private class GoalMap implements RowMapper<Goal>{

		@Override
		public Goal mapperRow(ResultSet rs) throws SQLException {
			Goal goal = new Goal();
			
			goal.setId(rs.getInt("id"));
			goal.setName(rs.getString("name"));
			goal.setProjectId(rs.getInt("t_project_id"));
			goal.setDesc(rs.getString("desc"));
			goal.setCreateTime(rs.getString("createtime"));
			
			return goal;
		}
		
	}

	public void addGoal(Goal g) {
		String sql = "INSERT INTO t_goal(name,`desc`,createtime,t_project_id) VALUES(?,?,?,?)";
		db.executeSQL(sql, g.getName(),g.getDesc(),g.getCreateTime(),g.getProjectId());
		
	}

	public Goal findGoalById(String curgid) {
		String sql = "SELECT id,name,t_project_id,`desc`,createtime from t_goal where id = ?";
		return db.executeQueryForObject(sql, new GoalMap(), curgid);
	}

	public void editGoal(Goal g) {
		String sql = "UPDATE t_goal SET name = ?,`desc` = ? where id = ?";
		db.executeSQL(sql, g.getName(),g.getDesc(),g.getId());
	}

	public List<Goal> findAllGoalByPid(int curpid) {
		String sql ="SELECT id,name,t_project_id,`desc`,createtime from t_goal where t_project_id = ? ORDER BY createtime DESC";
		return db.executeQueryForList(sql, new GoalMap(), curpid);
		
	}

	public List<Goal> findByEmployeeIdAndTaskId(int employeeId) {
		String sql = "SELECT a.id AS id, a.name AS name, a.`desc` AS `desc`, a.t_project_id AS t_project_id, a.createtime AS createtime FROM t_goal AS a, t_task WHERE a.id = t_task.t_goal_id AND t_employee_id = ?";
		return db.executeQueryForList(sql, new GoalMap(), employeeId);
	}
}
